// pages/schedule/schedule.js
const app = getApp();
import util from '../../../utils/util.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    refresh:false,
    pageEnd: false,
    param: {
      pageNumber: 1,
      pageSize: 10
    },
    list:[],
    listlength:[],
    typeShow:false,
  },
  user(e) {
    var that = this;
    var id = e.currentTarget.dataset.createuid;
    var name = e.currentTarget.dataset.name;
    wx.navigateTo({
      url: '/pages/myCardNew/myCardNew?type=visitor&id=' + id + '&name=' + name,
    })
  },
  org(e) {
    console.log('org')
    var dataset = e.currentTarget.dataset;
    wx.navigateTo({
      url: '/pages/orgHome/orgHome?id=' + dataset.orgid
    })
  },
  activityDetail: function (e) {
    var that = this;
    that.toggle('refresh',true)
    var id = e.currentTarget.dataset.id;
    var type = e.currentTarget.dataset.type;
    var actType = e.currentTarget.dataset.acttype;
    var url = "";
    if(type == 1){
      url = 'pages/register/register?id=' + id;
    } else if (type == 2){
      url = '/pages/hundredDetailed/hundredDetailed?id=' + id + "&type=" + actType;
    }
    console.log(id, type, actType)
    wx.navigateToMiniProgram({
      appId: type == 1 ? app.globalData.activityMiniAppId : app.globalData.blessingMiniAppId,
      envVersion: type == 1 ? app.globalData.activityMiniEnvVersion : app.globalData.blessingMiniEnvVersion,
      path: url,
      success(res) {
        console.log(res);
      }
    })
  },
  list: function (append) {
    var that = this;
  var param = that.data.param
    app.req({
      url: "orgmini/home/schedule",
      data: param,
      success: function (res) {
        if (200 == res.code) {
          var now = new Date()
          // var year = now.getFullYear()
          console.log(now)
          var list = res.data.rows
          for(var item of list){
            item.actStartTime = item.actStartTime.replace(/\-/g,"/")
            item.actEndTime = item.actEndTime.replace(/\-/g, "/")
            var dateS = new Date(item.actStartTime)
            var dateE = new Date(item.actEndTime)
            // item.index =  date.getTime();
            // if (date.getFullYear() == year){
            //   item.year = ''
            // }else{
            //   item.year = date.getFullYear()+'年'
            // }
           
            item.startWeek = util.getWeek(dateS)
            item.endWeek = util.getWeek(dateE)
            item.startDate = item.actStartTime.substring(5, 7) + '月' + item.actStartTime.substring(8, 10) + '日';
            item.endDate = item.actEndTime.substring(5, 7) + '月' + item.actEndTime.substring(8, 10) + '日';
            item.startTime = item.actStartTime.substring(11, 16);
            item.endTime = item.actEndTime.substring(11, 16);
            // if (item.actStartTime.substring(0, 10) == item.actEndTime.substring(0, 10)) {
            //   item.duration =  item.actStartTime.substring(11, 16) + '-' + item.actEndTime.substring(11, 16)
            // } else {
            //   item.duration = item.actStartTime.substring(5, 7) + '月' + item.actStartTime.substring(8, 10) + '日' + item.actStartTime.substring(11, 16) + '-' + item.actEndTime.substring(5, 7) + '月' + item.actEndTime.substring(8, 10) + '日' + item.actEndTime.substring(11, 16)
            // }
          }
          list.sort(that.sortRule)
          if (!list || list.length < param.pageSize) {
            that.toggle("pageEnd", true);
          }
          that.toggle("list", append ? that.data.list.concat(list) : list);
          console.log(that.data.list)
          for(var item of that.data.list){
            item.actStartDate = item.actStartTime.substring(0,10)
          }
          var arr = that.sortArr(that.data.list,'actStartDate')
          that.toggle("list", arr);
          that.toggle("listlength", res.data.rows);
          console.log('列表',that.data.list)
        }
      }
    });
  },
  sortRule(a, b) {
    return b.index - a.index;
  },
  // 传入一个数组
  // 按照特定方式格式化
  sortArr(arr, str) {
    console.log(arr)
    var _arr = [],
      _t = [],
      _tmp;
    arr = arr.sort(function (a, b) {
      var s = a[str],
        t = b[str];

      return s < t ? -1 : 1;
    });

    if (arr.length) {
      _tmp = arr[0][str];
    }
    for (var i in arr) {
      if (arr[i][str] === _tmp) {
        _t.push(arr[i]);
      } else {
        _tmp = arr[i][str];
        _arr.push(_t);
        _t = [arr[i]];
      }
    }
    _arr.push(_t);
    return _arr;
  },
  toggle(type, val) {
    this.setData({
      [type]: val
    });
  },
  showType() {
    getApp().mtj.trackEvent('yqlw_act_create');
    this.toggle("typeShow", true)
  },
  typeClose() {
    this.toggle("typeShow", false)
    this.toggle("wishShow", false)
  },
  mini: function (e) {
    var that = this;
    var d = e.currentTarget.dataset;
    if (d.type == 13) {
      this.toggle("typeShow", false)
      this.toggle("wishShow", true)
      return
    }
    if (d.type == 4) {
      getApp().mtj.trackEvent('yqlw_act_banquet');
    } else if (d.type == 3) {
      getApp().mtj.trackEvent('yqlw_act_sports');
    } else if (d.type == 8) {
      getApp().mtj.trackEvent('yqlw_act_meeting');
    } else if (d.type == 12) {
      getApp().mtj.trackEvent('yqlw_act_birth');
    } else if (d.type == 14) {
      getApp().mtj.trackEvent('yqlw_act_hundred');
    } else if (d.type == 15) {
      getApp().mtj.trackEvent('yqlw_act_banquet');
    }
    var url = "";
    if (d.turn) {
      url = d.turn
    } else {
      url = d.type == 12 ? 'pages/sport/sport' : 'pages/party/party';
    }
    url += "?type=" + d.type + "&name=" + d.name;
    console.log("组织url", url)

    wx.navigateToMiniProgram({
      appId: !Boolean(d.turn) ? app.globalData.activityMiniAppId : app.globalData.blessingMiniAppId,
      envVersion: !Boolean(d.turn) ? app.globalData.activityMiniEnvVersion : app.globalData.blessingMiniEnvVersion,
      path: url,
      success(res) {
        that.toggle("typeShow", false);
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    if(!that.data.refresh){
      app.getUserInfo(function (userInfo) {
        that.toggle("param.pageNumber", 1);
        that.toggle("pageEnd", false);
        that.list();
      });
    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this;
    app.getUserInfo(function (userInfo) {
      that.toggle("param.pageNumber", 1);
      that.toggle("pageEnd", false);
      that.list();
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (!that.data.pageEnd) {
      that.toggle("param.pageNumber", that.data.param.pageNumber + 1);
      that.list(true);
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})